// f4f.h
#ifndef _F4F_H
#define _F4F_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

typedef unsigned char BYTE;

// uuid
#include <uuid/uuid.h>

#define	UUID_MAX_LEN	40
#define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
#define GET_BE24(a,b,c) ((c) | ((b) << 8) | ((a) << 16))
#define GET_BE32(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24))
#define GET_BE16(a,b) ((b) | ((a) << 8))

#define F4F_MDAT_LEN 8
#define F4F_DEBUG 0

typedef struct F4F_TRUN
{
	uint32_t	sample_duration;
	uint32_t	sample_flags;
	uint32_t	sample_size;
	uint32_t	duration;
	uint64_t	pts;
} F4F_TRUN, *PF4F_TRUN;

typedef struct F4F_HEADER
{
	uint32_t	size;
	uint32_t	type;
	BYTE*		data;		// moof
} F4F_HEADER, *PF4F_HEADER;

typedef struct F4F_FORMAT
{	
	// file header
	PF4F_HEADER		header;

	// trun
	uint32_t		version;
	uint32_t		flags;
	uint32_t		entries;
	PF4F_TRUN		truns;

	// frame data postion
	uint32_t		mdat_pos;

	// file info
	char*			filename;

	// file pointer
	FILE*			pb;

	// pts
	uint64_t		last_pts;
	uint64_t 		total_duration;
	uint32_t		frame_index;

} F4F_FORMAT, *PF4F_FORMAT;

typedef struct F4F_PACKET
{
	uint64_t		pts;
	uint64_t		dts;
	BYTE*			data;
	uint32_t		size;
} F4F_PACKET, *PF4F_PACKET;


void		f4f_close(PF4F_FORMAT p);
PF4F_FORMAT	f4f_open_file(const char* filename, uint64_t start_time);
int 		f4f_read_frame(PF4F_FORMAT p, char* buffer, int read_bytes);
int64_t		f4f_seek_pos(PF4F_FORMAT p, int64_t pos);
int 		f4f_get_next_frame(PF4F_FORMAT p, PF4F_PACKET pkt);

int32_t		f4f_init_packet(PF4F_PACKET p);
int32_t		f4f_release_packet(PF4F_PACKET p);
// utility
void print_hex(unsigned char* buffer, int size);
void print_ascii(unsigned char* buffer, int size);


#endif // _F4F_H